import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-03-10
 * Time: 17:17
 */
public class Test {
    //求数组的平均值
    public static double ave(double[] arr){
        double sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum / arr.length;
    }
    public static void main8(String[] args) {
        double[] arr = { 1 , 2 ,3 ,4 ,5};
        double ret = ave(arr);
        System.out.println(ret);
    }
    //实现toSting的原理
    public static String myToSting(int[] arr){
        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            str += arr[i];
            if(i != arr.length - 1){
                str += ", ";
            }
        }
        str += "]";
        return str;
    }


    public static void main7(String[] args) {
        int[] arr = { 1 ,  2,  3, 4, 5};
        System.out.println(myToSting(arr));
    }
    //空指针null
    public static void main6(String[] args) {
        int[] arr = null;
        //c语言中arr指向0和地址
        //java中arr不指向任何对象
        System.out.println(arr[0]);
        System.out.println(arr.length);
        //都会爆出一个错误空指针异常
        //null没有长度，也没有内容
    }
    //对数每一个数扩大二倍
    public static int[] func1(int[] arr){
        int[] ret = new int[arr.length];
        for (int i = 0; i < arr.length ; i++) {
            ret[i] = arr[i] * 2;
        }
        return ret;
    }
    public static void func2(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] * 2;
        }
    }
    public static void main5(String[] args) {
        int[] arr = { 1 , 2 , 3 , 4 ,5};
        int[] tmp = func1(arr);
        //不该arr的内容
        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(tmp));
        func2(arr);
        //改掉arr的内容
        System.out.println(Arrays.toString(arr));
    }
    //数组对象
    public static void main4(String[] args) {
        int[] arr1 = new int[3];
        arr1[0] = 10;
        arr1[1] = 20;
        arr1[2] = 30;
        int[] arr2 = new int[]{1 , 2 , 3 , 4 , 5};
        arr2[0] = 100;
        arr2[1] = 200;
        arr2[2] = 300;
        arr1 = arr2;//arr1这个引用 指向了arr2这个引用指向的对象
        arr1[3] = 400;
        arr1[4] = 500;
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
    }
    //求三个数的最大值
    public static int max1(int a , int b){
        return a>b?a:b;
    }
    public static int max2(int a , int b , int c){
        int max = max1(a , b);
        int ret = max1(max , c);
        return ret;
    }
    public static void main3(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        int m = max2(a , b , c);
        System.out.println(m);

    }
    //遍历数组(三种方法)
    public static void main2(String[] args) {
        int[] arr = { 1 , 2, 3, 4, 5 ,6};
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        for (int x: arr) {
            System.out.print(x + " ");//无法获取对应的下标
        }
        System.out.println();
        //数组操作的工具类 Arrays
        String ret = Arrays.toString(arr);//将对应的数组转化为字符串
        System.out.println(ret);
    }
    //数组的定义与初始化
    public static void main1(String[] args) {
        int[] arr = { 1, 2, 3, 4, 5};
        int[] arr2 = new int[]{1 , 2, 3, 4, 5};
        int[] arr3 = new int[10];//自动初始化为0
        int[] arr4;
        arr4 = new int[10];
        int[] arr5;
        arr5 = new int[]{1 , 2, 3 ,4};
        //int[] arr4 = new int[3]{ 1 , 2 , 3};错误后面不能加[]
        /*int[] arr5;
        arr5 = {1 , 2 , 3 ,4 ,5}; 错误 定义初始化不能分开*/
        boolean[] flag = new boolean[3];//自动初始化为false
    }
}
